#include "BlockQueue.hpp"
#include "Task.cc"
#include <time.h>
#include <cstdlib>
#include <unistd.h>
#include <string>
using namespace ns_blockqqueue;
using namespace ns_task;
void* consumer(void* args)
{
    BlockQueue<Task>* bq = (BlockQueue<Task>*)args;
    while(true)
    {
      Task t;
      bq->Pop(&t);
      t();
    }
}
void* produter(void* args)
{
    BlockQueue<Task>* bq = (BlockQueue<Task>*)args;
    while(true)
    {
        string ops = "+-*/%";
        int x = rand() % 20 + 1;
        int y = rand() % 20 + 1;
        char op = ops[rand() % 5];
        Task t(x, y, op);
        cout << "生产者派发了一个任务：" << x << op << y << "= ?" << endl;
        bq->Push(t);
        sleep(1);
    }
}

int main()
{
    srand((long long)time(nullptr));
    BlockQueue<Task>* bq = new BlockQueue<Task>();
    pthread_t c, p;
    pthread_t c1, c2, c3, c4;

    pthread_create(&c, nullptr, consumer,(void*)bq);
    pthread_create(&c1, nullptr, consumer,(void*)bq);
    pthread_create(&c2, nullptr, consumer,(void*)bq);
    pthread_create(&c3, nullptr, consumer,(void*)bq);
    pthread_create(&c4, nullptr, consumer,(void*)bq);
    pthread_create(&p, nullptr, produter,(void*)bq);

    pthread_join(c, nullptr);
    pthread_join(c1, nullptr);
    pthread_join(c2, nullptr);
    pthread_join(c3, nullptr);
    pthread_join(c4, nullptr);
    pthread_join(p, nullptr);

    return 0;
}
